package com.shop.service.impl;

import com.shop.bean.Orders;
import com.shop.bean.Product;

import com.shop.bean.Product_images;
import com.shop.dao.CategoryDao;
import com.shop.dao.OrdersDao;
import com.shop.dao.ProductDao;
import com.shop.service.IProductService;
import com.shop.util.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    ProductDao productDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    OrdersDao orderDao;




    @Override
    public int addProduct(Product product) {
        return productDao.addProduct(product);
    }

    @Override
    public void addProductWithImages(Product product) {
        // 插入商品信息
        productDao.addProduct(product);



        // 获取图片信息
        List<Product_images> productImages = product.getImgurl();
        int productId = product.getId();


        // 插入图片信息
        if (productImages != null && !productImages.isEmpty()) {
            productDao.addProductImages(productId, productImages);
        }
    }

    @Override
    public void editProductWithImages(Product product){
        productDao.deleteImg(product.getId());
        // 插入商品信息
        productDao.editProduct(product);
        int productId = product.getId();

        // 获取图片信息
        List<Product_images> productImages = product.getImgurl();

        // 插入图片信息
        if (productImages != null && !productImages.isEmpty()) {

            productDao.editProductImages(productId, productImages);
        }
    }

    @Override
    public int deleteProduct(int id) {
        return productDao.deleteProduct(id);
    }

    @Override
    public int putOn(int id){return productDao.putOn(id);}

    @Override
    public int putOff(int id){System.out.println(id);return productDao.putOff(id);}

    @Override
    public int editProduct(Product product) {
        return productDao.editProduct(product);
    }

    @Override
    public Product getProductById(int id) {
        return productDao.getProductById(id);
    }


    @Override
    public List<Product> selectProducts  (Page page, String text) {
        return productDao.selectProducts(page, text);
    }

    @Override
    public List<Product> getProductByCategoryAndName(Product product, Page page, String text){

        return productDao.getProductByCategoryAndName(product,page,text);
    }

    @Override
    public List<Product> getNew8() {
        // 假设 productDao.selectProducts 方法返回的是 List<Product> 类型
        List<Product> productList = productDao.selectProducts(null, null);

        // 使用 Stream API 对商品列表进行排序和截取
        return productList.stream()
                .sorted(Comparator.comparing(Product::getCreated_at).reversed()) // 按 created_at 降序排序
                .limit(8) // 取前八个商品
                .collect(Collectors.toList()); // 转换为列表
    }

    @Override
    public List<Product> getChildren(int id) {
        return productDao.getChildren(id);
    }

    @Override
    public int getCount(String text) {
        return productDao.getCount(text);
    }

    @Override
    public int getCounts(String text,Product product) {
        return productDao.getCounts(text,product);
    }

    @Override
    public int decStock(Integer productid,Integer quantity) {

        Product product = productDao.getProductById(productid);
        if (product.getStock() < quantity) {
            throw new RuntimeException ("库存不足！！");
        }
        quantity =  product.getStock()-quantity;
        return productDao.decStock(productid,quantity);
    }


}

